route.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { auth } from "../../auth";
  3. import { getServerSideConfig } from "@/app/config/server";
  4. import { GEMINI_BASE_URL, Google, ModelProvider } from "@/app/constant";
  5. async function handle(
  6. req: NextRequest,
  7. { params }: { params: { path: string[] } },
  8. ) {
  9. console.log("[Google Route] params ", params);
  10. if (req.method === "OPTIONS") {
  11. return NextResponse.json({ body: "OK" }, { status: 200 });
  12. }
  13. const controller = new AbortController();
  14. const serverConfig = getServerSideConfig();
  15. let baseUrl = serverConfig.googleUrl || GEMINI_BASE_URL;
  16. if (!baseUrl.startsWith("http")) {
  17. baseUrl = `https://${baseUrl}`;
  18. }
  19. if (baseUrl.endsWith("/")) {
  20. baseUrl = baseUrl.slice(0, -1);
  21. }
  22. let path = `${req.nextUrl.pathname}`.replaceAll("/api/google/", "");
  23. console.log("[Proxy] ", path);
  24. console.log("[Base Url]", baseUrl);
  25. const timeoutId = setTimeout(
  26. () => {
  27. controller.abort();
  28. },
  29. 10 * 60 * 1000,
  30. );
  31. const authResult = auth(req, ModelProvider.GeminiPro);
  32. if (authResult.error) {
  33. return NextResponse.json(authResult, {
  34. status: 401,
  35. });
  36. }
  37. const bearToken = req.headers.get("Authorization") ?? "";
  38. const token = bearToken.trim().replaceAll("Bearer ", "").trim();
  39. const key = token ? token : serverConfig.googleApiKey;
  40. if (!key) {
  41. return NextResponse.json(
  42. {
  43. error: true,
  44. message: `missing GOOGLE_API_KEY in server env vars`,
  45. },
  46. {
  47. status: 401,
  48. },
  49. );
  50. }
  51. const fetchUrl = `${baseUrl}/${path}?key=${key}`;
  52. const fetchOptions: RequestInit = {
  53. headers: {
  54. "Content-Type": "application/json",
  55. "Cache-Control": "no-store",
  56. },
  57. method: req.method,
  58. body: req.body,
  59. // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
  60. redirect: "manual",
  61. // @ts-ignore
  62. duplex: "half",
  63. signal: controller.signal,
  64. };
  65. try {
  66. const res = await fetch(fetchUrl, fetchOptions);
  67. // to prevent browser prompt for credentials
  68. const newHeaders = new Headers(res.headers);
  69. newHeaders.delete("www-authenticate");
  70. // to disable nginx buffering
  71. newHeaders.set("X-Accel-Buffering", "no");
  72. return new Response(res.body, {
  73. status: res.status,
  74. statusText: res.statusText,
  75. headers: newHeaders,
  76. });
  77. } finally {
  78. clearTimeout(timeoutId);
  79. }
  80. }
  81. export const GET = handle;
  82. export const POST = handle;
  83. export const runtime = "edge";
  84. export const preferredRegion = [
  85. "bom1",
  86. "cle1",
  87. "cpt1",
  88. "gru1",
  89. "hnd1",
  90. "iad1",
  91. "icn1",
  92. "kix1",
  93. "pdx1",
  94. "sfo1",
  95. "sin1",
  96. "syd1",
  97. ];